{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "tags": [
     "remove-cell"
    ]
   },
   "outputs": [],
   "source": [
    "# Reference: https://jupyterbook.org/interactive/hiding.html\n",
    "# Use {hide, remove}-{input, output, cell} tags to hiding content\n",
    "\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "%matplotlib inline\n",
    "import ipywidgets as widgets\n",
    "from ipywidgets import interact, interactive, fixed, interact_manual\n",
    "from IPython.display import display\n",
    "\n",
    "sns.set()\n",
    "sns.set_context('talk')\n",
    "np.set_printoptions(threshold=20, precision=2, suppress=True)\n",
    "pd.set_option('display.max_rows', 7)\n",
    "pd.set_option('display.max_columns', 8)\n",
    "pd.set_option('precision', 2)\n",
    "# This option stops scientific notation for pandas\n",
    "# pd.set_option('display.float_format', '{:.2f}'.format)\n",
    "\n",
    "def display_df(df, rows=pd.options.display.max_rows,\n",
    "               cols=pd.options.display.max_columns):\n",
    "    with pd.option_context('display.max_rows', rows,\n",
    "                           'display.max_columns', cols):\n",
    "        display(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(sec:theory_probIntro)=\n",
    "# Probability Distribution for a Statistic\n",
    "\n",
    "In this chapter, we used probability to find chances related to simple random samples, randomized controlled experiments, and measurement error. These probability frameworks enable us to run simulation studies for a hypothetical survey, an experiment, or some other chance process in order to study their behavior. For example, we found the expected outcome of a clinical trial for a vaccine under the assumption that the treatment was not effective; and, we studied the support for Clinton and Trump in a simple random sample that used the actual votes cast in the election. The simulation studies enabled us to quantify the typical deviations from the expected outcome and the distribution of the possible values for the summary statistics.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "In this section, we formalize the notions of expected value, standard deviation, and probability distribution.\n",
    "\n",
    "Our examples reduced the data to a summary statistic, and we used simulation to approximate the random behavior of the statistic.  That is, we summarized the possible values for the statistic in a table with the proportion of simulations that yielded the value, and we found the average value and standard deviation of the values. In essence, the simulations were approximating probability calculations.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "We formalize these calculations here.  Our statistic is based on data $x_1, x_2, \\ldots, x_n$, which we refer to as $T(x_1, \\ldots , x_n)\\textrm{ or }T$ for short. The probability distribution of the statistic is\n",
    "\n",
    "$$\\mathbb{P}(T(x_1, \\ldots, x_n) = t) = p_t,$$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "for all possible values $t$ that the statistic could take. \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "The expected value of $T$ is the average value, i.e.,\n",
    "\n",
    "$$\\mathbb{E}(T(x_1, \\ldots, x_n) = \\sum_t t\\mathbb{P}(T(x_1, \\ldots, x_n) = t) = \\sum_t tp_t$$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "And the standard error of the statistic is the standard deviation:\n",
    "\n",
    "$$\\mathbb{SE}(T) = \\sqrt{\\sum_t (t-\\mathbb{E}(T))^2 p_t}$$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "Except for very simple situations, we will rely on simulation studies to estimate the probability distribution of a statistic and its expected value and standard error.\n",
    "\n",
    "Recall in the Section on Sampling Distributions, the small example examine a statistic that was the   the proportion of dogs in a sample of 3 animals from a population of 4 dogs and 3 cats.  We derived the probability distribution of the statistic: \n",
    "\n",
    "\n",
    "| Sample Proportion | No. Occurrences | Chance |\n",
    "| :---: | :---: | :---: |\n",
    "| 1 | 4 | 4/35 |\n",
    "| 2/3 | 18 | 18/35 |\n",
    "|1/3 | 12 | 12/35 |\n",
    "| 0 | 1 | 1/35 |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are only 4 possible values for $T$, and they are $0$, $1/3$, $2/3$, and $1$. The expected value and standard error of the statistic were:\n",
    "\n",
    "$${\\mathbb{E}}(T) = (1 \\times \\frac{4}{35}) + (\\frac{2}{3}\\times \\frac{18}{35}) + (\\frac{1}{3} \\times \\frac{12}{35}) + (0 \\times \\frac{1}{35}) =~ \\frac{4}{7}$$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "$${\\mathbb{S}}(T) = \\sqrt{(1-\\frac{4}{7})^2\\times \\frac{4}{35} + \\cdots +(0-\\frac{4}{7})^2\\times \\frac{1}{35} } \\approx 0.233$$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "We can generalize these results for an urn model where the random sampling is either with or without replacement and the statistics is the average of the values on the marbles drawn. Suppose the population/urn consists of values $x_1 , x_2, \\ldots , x_N$, and we take $n$ draws from the urn. For $T$ the sample average when the draws are with replacement, we have \n",
    "\n",
    "$$\\mathbb{E}(T) = \\frac{x_1 + x_2 + \\cdots + x_N}{N} = \\textrm{ population average}$$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "and \n",
    "\n",
    "$${\\mathbb{S}}(T) = \\frac{1}{\\sqrt{n}}\\sqrt{[(x_1-\\mathbb{E}(T))^2 + \\cdots +(x_N-\\mathbb{E}(T))^2]/N} \\\\ = \\frac{1}{\\sqrt{n}} \\textrm{ population standard deviation} $$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "In the case of draws without replacement, the expected value is the same, i.e., it matches the population average, and the standard error decreases by a factor of $\\sqrt{(N-n)/(N-1)}$; that is, \n",
    "\n",
    "$${\\mathbb{S}}(T_{SRS}) = \\sqrt{\\frac{N-n}{N-1}} ~\\frac{1}{\\sqrt{n}}~\\textrm{ population standard deviation}. $$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "When the population size is large compared to the sample size, we ignore this factor and treat the sampling as if it were with replacement. \n",
    "\n",
    "Note this situation covers the case of a sample proportion because a proportion is simply the average of 0s and 1s.\n",
    "\n",
    "In practice, we typically don’t know the population quantities so we approximate the population average with the sample average and the population standard deviation with the sample standard deviation.\n"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
